<?php
function edit(){
    $id = $_GET['id'];
    // echo $id;
    // 获取数据
    $name = $_POST['name'];
    // echo $name;
    $gender = $_POST['gender'];
    $birthday = $_POST['birthday'];
    $avatar = $_FILES['avatar'];
    // var_dump($avatar);
    /*李四array(5) {
      ["name"]=&gt;
      string(8) "test.jpg"
      ["type"]=&gt;
      string(10) "image/jpeg"
      ["tmp_name"]=&gt;
      string(27) "C:\Windows\Temp\phpD649.tmp"
      ["error"]=&gt;
      int(0)
      ["size"]=&gt;
      int(18516)
    } */
    // 1.连接数据库
    $connect = mysqli_connect('localhost','root','123456','users');
    // var_dump($connect);
    // 判断
    if (!$connect) {
      exit('<h1>连接数据库失败</h1>');
    }
  
    // 2.执行数据查询操作
    $query1 = mysqli_query($connect,'select * from info where id='.$id.';');
    // 判断
    if (!$query1) {
      exit('<h1>编辑失败</h1>');
    }
    // 获取查询数据
    $original = mysqli_fetch_assoc($query1);
    print_r($name.$gender.$birthday);
    var_dump($original);
    // 判断数据是否更改
    if($name != $original['name']) {
      mysqli_query($connect,"update info set name='{$name}' where id='{$id}';");
    }
    if($gender != $original['gender']) {
      mysqli_query($connect,"update info set gender='{$gender}' where id='{$id}';");
    }
    if($birthday != $original['birthday']) {
      mysqli_query($connect,"update info set birthday='{$birthday}' where id='{$id}';");
    }    
    if($avatar['name']) {
      // echo "<script>alert('图片被更新')</script>";
      // 不推荐文件名中含有中文
      $src = './assets/img/'.uniqid().mb_convert_encoding($avatar['name'],'GBK','UTF-8');//相对路径
      $back = move_uploaded_file($avatar['tmp_name'], $src);
      if (!back) {
        exit("图片上传失败");
      }      
      mysqli_query($connect,"update info set avatar='{$src}' where id='{$id}';");
    }  
    // 3.关闭连接通道
    mysqli_close($connect);
  
    // 4.页面跳转
    header('Location:index.php');
  }
  
// 表单是否提交
if ($_SERVER['REQUEST_METHOD'] === "POST") {
    edit();
}
?>